package com.shuoyue.ycgk.ui.recruitment.resume.certificate;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.impl.RefreshFooterWrapper;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Cert;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.utils.StringUtil;
import com.shuoyue.ycgk.views.dialog.CustomPicker;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CertificateActivity extends BaseMvpActivity<CertContract.Presenter>
        implements CertContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshlayout;


    CertAdapter adapter;

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_certificate;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        pageTitle.setText("我的证书");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        adapter = new CertAdapter(null);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Cert type = (Cert) adapter1.getItem(position);
            HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "删除",
                    "是否删除证书" + type.getCertName());
            hintConfirmDialog.setOkListener(v -> {
                mPresenter.delete(type);
                hintConfirmDialog.dismiss();
            });
            hintConfirmDialog.show();
        });
        recyclerView.setAdapter(adapter);

        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.query();
            refreshlayout.finishRefresh();
        });
        registEmptyView(adapter);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new CertContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.query();
    }

    @OnClick({R.id.back, R.id.type, R.id.name, R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.type:
                showTypeSelect();
                break;
            case R.id.name:
                String typeName = type.getText().toString();
                if (!TextUtils.isEmpty(typeName)) {
                    showCertSelect(getChild(typeName), typeName);
                }
                break;
            case R.id.add:
                String certName = name.getText().toString();
                if (!TextUtils.isEmpty(certName)) {
                    mPresenter.save(certName);
                }
                break;
        }
    }

    @Override
    public void saveSuc() {
        mPresenter.query();
    }

    @Override
    public void querySuc(List<Cert> list) {
        adapter.setNewData(list);
    }

    @Override
    public void deleteSuc(Cert cert) {
        adapter.remove(adapter.getData().indexOf(cert));
    }

    /**
     * 类型选择
     */
    void showTypeSelect() {
        String[] dataArray = new String[]{"英语类", "医护类", "财会类", "建筑规划类", "司法类", "教师类", "消防安全类", "计算机类", "通用技能类"};
        CustomPicker picker = new CustomPicker(this, dataArray, "证书类型");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            if(!option.equals(type.getText())){
                type.setText(option);
                name.setText("");
            }

        });
        picker.show();
    }

    /**
     * 证书选择
     */
    void showCertSelect(String[] certs, String title) {
        CustomPicker picker = new CustomPicker(this, certs, title);
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            name.setText(option);
        });
        picker.show();
    }


    private String[] getChild(String name) {
        if (name.equals("英语类")) {
            return new String[]{"大学英语四级证书", "大学英语六级证书", "专业学英语四级证书", "专业学英语八级证书", "英语口语/口译资格证书", "托福/雅思"};
        } else if (name.equals("医护类")) {
            return new String[]{"职业医师证", "职业助理医师证"};
        } else if (name.equals("财会类")) {
            return new String[]{"会计从业资格证", "注册会计师"};
        } else if (name.equals("建筑规划类")) {
            return new String[]{"一级建造师", "二级建造师", "城市规划师", "造价员", "造价师"};
        } else if (name.equals("司法类")) {
            return new String[]{"法律职业资格证/律师职业资格证"};
        } else if (name.equals("教师类")) {
            return new String[]{"教师资格证(幼儿园)", "教师资格证(小学)", "教师资格证(初中)", "教师资格证(高中)"};
        } else if (name.equals("消防安全类")) {
            return new String[]{"安全工程师", "消防工程师"};
        } else if (name.equals("计算机类")) {
            return new String[]{"全国计算机等级考试证书(一级)", "全国计算机等级考试证书(二级)"};
        } else if (name.equals("通用技能类")) {
            return new String[]{"普通话二甲证书", "普通话二乙证书"};
        } else
            return new String[]{};
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        List<Cert> certs = adapter.getData();
        if (certs.size() == 0) {
            intent.putExtra("name", "");
        } else {
            intent.putExtra("name", certs.get(0).getCertName() + "等" + certs.size() + "个证书");

        }
        setResult(RESULT_OK,intent);
        super.finish();
    }
}
